package service;


import dao.UserDao;
import entity.Result;
import entity.User;
import org.springframework.stereotype.Service;

/**
 * @program: IoT201759225309
 * @description: 用于提供User操作
 * @author: Xiaozhuoer
 * @create: 2021-06-01 14:23
 **/

@Service
public class UserService {
    private final UserDao userDao;
    public UserService(UserDao userDao){
        this.userDao = userDao;
    }

    /**
     * 用户注册
     * @param user
     * @return result
     */
    public Result logonUser(User user){
        Result result = new Result();
        int  affectedRowCount= userDao.logonUser(user);
        if(affectedRowCount == 1){
            result.setStatus(0);
            result.setMsg("用户注册成功");
        }else{
            result.setMsg("用户注册失败");
        }
        return result;
    }
    /**
     * 用户登录
     * @param user
     * @return result
     */
    public Result loginUser(User user) {
        Result result = new Result();
        User loginResult = userDao.loginUser(user);
        if(loginResult == null){
            user.setEmail(user.getUsername());
            user.setUsername(null);
            loginResult = userDao.loginUser(user);
        }
        if(loginResult != null){
            result.setStatus(0);
            result.setMsg("用户登录成功");
            result.setData(loginResult);
        }else{
            result.setMsg("用户登录失败");
            result.setData(null);
        }
        return result;
    }
    /**
     * 通过Email获取用户信息
     * @param user
     * @return result
     */
    public Result findByEmail(User user){
        Result result = new Result();
        user = userDao.findUserByEmail(user);
        if(user.getUsername().equals(null)){
            result.setMsg("邮箱不存在");
        }else{
            result.setData(user);
        }
        return result;
    }
    /**
     * 获取所有用户信息
     * @return result
     */
    public Result findAll() {
        Result result = new Result();
        result.setData(userDao.findAll());
        return result;
    }
    /**
     * 修改用户信息
     * @param user
     * @return result
     */
    public Result modifyUser(User user) {
        Result result = new Result();
        result.setData(userDao.modifyUser(user));
        return result;
    }
    /**
     * 删除用户信息
     * @param user
     * @return result
     */
    public Result deleteUser(User user) {
        Result result = new Result();
        result.setData(userDao.deleteUser(user));
        return result;
    }
    /**
     * 修改用户邮件信息
     * @param user
     * @return result
     */
    public Result modifyEmail(User user) {
        Result result = new Result();
        result.setData(userDao.modifyEmail(user));
        return result;
    }
    /**
     * 修改用户Type信息
     * @param user
     * @return result
     */
    public Result modifyType(User user) {
        Result result = new Result();
        result.setData(userDao.modifyType(user));
        return result;
    }
    /**
     * 通过ID查找用户信息
     * @param id
     * @return User
     */
    public User findUserById(int id){
        return userDao.findUserById(id);
    }
}
